// pages/outvisit/task/taskDetails.js
var api = require("../../../utils/api.js");
var util = require("../../../utils/util.js");
var common = require("../../../utils/common.js");
const pagetitle = ["", "任务详情", "任务详情", "任务详情", "任务详情"];
const taskDetailsTitles = common.taskDetailsTitles;//详情字段
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,//用户ID
    type: 0,//任务类型
    id: 0,//任务ID
    taskDetailsIsHide:true,//任务详情是否隐藏
    taskDetailsList: [],//任务详情列表
    causeState:1,//外访信息状态
    operationIsHide:true,//下方操作按钮是否隐藏
    stopOutvisitFormIsHide:true,//终止任务表单是否隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    
    var that = this;
    var userId = wx.getStorageSync("userId");
    var id = options.id;
    var outvisitTime = options.outvisitTime;
    var taskLongitude = options.taskLongitude;
    var taskLatitude = options.taskLatitude;
    var taskAddress = options.taskAddress;
    // 设置标题
    wx.setNavigationBarTitle({
      title: '预约详情'
    });
    // 设置任务类型和ID
    that.setData({
      userId: userId,
      id: id,
    }, () => {
      // 获取任务详情
      that.getAppointmentDetails();
    });
  },

  // 获取任务详情
  getAppointmentDetails:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    console.log("id==================>"+that.data.id);
    api.getAppointmentDetails(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();

        var data = res.data;
         
        // 更新数据
        that.setData({
          detail: data,
        }, () => {
          // 合并录音
        });
      }
    });
  },

  // 获取年龄
  getAge: function(idCard) {
    var age = "";
    var tmpStr = "";
    if (idCard.length == 15) {
      tmpStr = "19" + idCard.substring(6, 8);
      age = new Date().getFullYear() - parseInt(tmpStr) + "岁";
    }else if(idCard.length == 18){
      tmpStr = idCard.substring(6, 10);
      age = new Date().getFullYear() - parseInt(tmpStr) + "岁";
    }
    return age;
  },
  
  // 获取性别
  getSex: function(idCard) {
    var sex = "";
    if(idCard && idCard.length >= 18){
      var sexNum = idCard.substring(16, 17);
      if (sexNum != "*") {
        if (sexNum % 2 == 0) {
            sex = "女";
        } else {
            sex = "男";
        }
      }
    }
    return sex;
  },

  // 录入信息
  returnInfo:function(e){
    var that = this;
    wx.navigateTo({
      url: 'returnInfo?id=' + e.currentTarget.dataset.id
    });
  },

  // 预约信息
  appointmentInfo:function(e){
    var that = this;
    wx.navigateTo({
      url: 'appointmentInfo?id=' + e.currentTarget.dataset.id
    });
  },

  // 提交表单
  formSubmit: function (file) {
    var that = this;
    api.getOutvistRecord(that.data.id).then(res => {
      if (res.code == 0) {
        var data = res.data;
        var submitData = {};
        if (data) {
          var submitData = data;
        }
        submitData.outvisitTaskId = that.data.id;
        var files = submitData.files;
        if (!files) {
          files = [];
        }
        files.push(file);
        submitData.files = files;
        api.setOutvistRecord(submitData).then(res2 => {
          if (res2.code == 0) {
            wx.hideLoading();//隐藏加载中
            // 提示
            if (that.data.type == 1) {
              wx.showToast({
                title: '录入信息前记得打卡哦',
                icon: 'none'
              });
            }
          }
        });
      }
    });
  },

  // 设置下方操作按钮是否隐藏
  setOperationIsHide: function (type, outvisitTime) {
    var that = this;
    var operationIsHide = true;
    if (type == 1 || type == 2) {
      operationIsHide = false;
    }
    if (type == 3) {
      var myDate = Date.parse(new Date());
      var curTime = util.formatTimeTT(myDate, "Y-M-D");
      if (outvisitTime == curTime){
        operationIsHide = false;
      }
    }
    that.setData({
      operationIsHide: operationIsHide
    });
  },

  // 详情按钮点击事件
  detailsBtnClick: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.id;
    var url="";
    switch(type){
      case '1':
        url = "collectionRecord";
        break;
      case '2':
        url = "totalDebtDetails";
        break;
      default:
        break;
    }
    wx.navigateTo({
      url: url + '?id=' + that.data.collectionCaseId
    });
  },

  // 外访信息切换
  causeTabClick: function (e){
    this.setData({
      causeState: e.currentTarget.dataset.id
    });
  },

  // 任务详情收起
  packUpTask:function(){
    var that=this;
    this.setData({
      taskDetailsIsHide: that.data.taskDetailsIsHide?false:true
    });
  },

  /**
   * 下方操作按钮点击事件
   */
  // 打卡
  checkin: function () {
    var that = this;
    wx.navigateTo({
      url: 'signIn?id=' + that.data.id + '&taskLongitude=' + that.data.taskLongitude + '&taskLatitude=' + that.data.taskLatitude + '&taskAddress=' + that.data.taskAddress
    });
  },

  telephone: function (option) {
    var telnumber = option.currentTarget.dataset.telephone;
    wx.makePhoneCall({
      phoneNumber: telnumber
    })
  },

  // 录入信息
  inputInfo:function(){
    var that = this;
    wx.navigateTo({
      url: 'inputInfo?id=' + that.data.id + '&collectionCaseId=' + that.data.collectionCaseId + '&trustor=' + that.data.trustor
    });
  },

  // 终止任务
  stopOutvisit: function (e) {
    var that = this;
    var cause = e.detail.value.cause;
    if (!cause.trim()){
      wx.showToast({
        title: "终止原因不能为空",
        icon: "none"
      });
      return;
    }
    api.stopOutvisit(that.data.id, cause);
  },

  // 终止任务表单显示与隐藏
  stopOutvisitFormShowOrHide:function(){
    var stopOutvisitFormIsHide=this.data.stopOutvisitFormIsHide;
    if (stopOutvisitFormIsHide) {
      stopOutvisitFormIsHide=false;
    } else {
      stopOutvisitFormIsHide = true;
    }
    this.setData({
      stopOutvisitFormIsHide: stopOutvisitFormIsHide
    })
  },

  // 完成任务
  completeOutvisit: function () {
    var that = this;
    wx.showModal({
      title: "提示",
      content: "确定要完成任务吗？",
      success: function (sm) {
        if (sm.confirm) {
          api.completeOutvisit(that.data.id);
        }
      }
    })
  },

  // 抢单
  scrambleAlone: function () {
    var that = this;
    wx.showModal({
      title: "提示",
      content: "确定要抢单吗？",
      success: function (sm) {
        if (sm.confirm) {
          api.scrambleAlone(that.data.id);
        }
      }
    })
  },

  // 回访
  returnVisits: function () {
    var that = this;
    wx.showModal({
      title: "提示",
      content: "确定要回访吗？",
      success: function (sm) {
        if (sm.confirm) {
          api.returnVisits(that.data.id);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})